package com.ymatou.iapi.payment.testcase;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.payment.parameter.AcquireOrderBean;
import com.ymatou.iapi.payment.parameter.AcquireRefundBean;
import com.ymatou.iapi.payment.parameter.GodRefundCompensateBean;
import com.ymatou.iapi.payment.service.GodRefundCompensateCall;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.database.model.MPayment;
import com.ymttest.database.model.MRefund;
import com.ymttest.database.sqlwapper.PayGateWayWapper;
import com.ymttest.utils.TestDataManager;
/**
*神器退款补偿job
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_GodRefundCompensate{
	private static GodRefundCompensateBean godrefundcompensateBean;
	private static GodRefundCompensateCall godrefundcompensateCall;
	private static PayGateWayWapper pdw;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("神器退款补偿job");
	}

	@Before
	public void caseUp() {
		godrefundcompensateBean=new GodRefundCompensateBean();
		godrefundcompensateCall=new GodRefundCompensateCall();
		pdw=new PayGateWayWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(godrefundcompensateCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private MPayment getPPPayment(AcquireOrderBean aqb) {

		MPayment ppm = new MPayment();
		try {
			ppm = pdw.selectPaymentByTradingid(aqb.getOrderId()).get(0);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		return ppm;
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GodRefundCompensate_001() {
		Logger.start(true,"神器退款补偿job成功");
		try {
			//UPDATE refund SET refund.RefundStatus=4 WHERE RefundStatus = 0 and ApproveStatus = 1 and IsCancel= 0
			//测试环境退款记录积压 可以使用sql更新
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6000");
			aqb.setPayPrice("6.00");
			String tn = PayGateWayCallService.acquireOrder(aqb).getString("Result");
			PayGateWayCallService.notifyCall(aqb);
			AcquireRefundBean ar = PayGateWayCallService.AcquireRefund(aqb, "5.00");
			PayGateWayCallService.approveRefund(ar.getTradeNo());
			Logger.comment("查询部分退款单号");
			MPayment ppm = this.getPPPayment(aqb);
			MRefund mrr = pdw.selectRefundRequestByPaymentId(ppm.getPaymentId()).get(0);
			String refundbn = mrr.getRefundBatchNo().toString();
			String refundid = mrr.getRefundId().toString();
			pdw.updateRefundStatus(refundbn, "0");
			List<MRefund> list = pdw.selectRefundRequestByTraceId(ar.getRefundNo());
			godrefundcompensateCall.addHeader("Mock", "1");
			godrefundcompensateCall.addHeader("MockResult-Hipo-is_success", "true");
			String mockid = TestDataManager.getRandomUUID().toString();
			godrefundcompensateCall.addHeader("MockId", mockid);
			godrefundcompensateBean.setAppId("job.iapi.ymatou.com");
			godrefundcompensateCall.setData(godrefundcompensateBean);
			godrefundcompensateCall.callService();		
			Thread.sleep(2000);
			list = pdw.selectRefundRequestByTraceId(ar.getRefundNo());
			Logger.verifyEquals(1, list.get(0).getRefundStatus(), "提交第三方退款成功");
//			查询退款单提交第三方
//			select RefundId from refund where paytype = 6000 and CreateTime > date_sub(now(), interval 30 day)
//					and RefundStatus = 0 and ApproveStatus = 1 and IsCancel= 0 limit 30

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GodRefundCompensate_002() {
		Logger.start(true,"神器退款补偿job成功-6103");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6103");
			aqb.setPayPrice("6.00");
			String tn = PayGateWayCallService.acquireOrder(aqb).getString("Result");
			PayGateWayCallService.notifyCall(aqb);
			AcquireRefundBean ar = PayGateWayCallService.AcquireRefund(aqb, "5.00");
			PayGateWayCallService.approveRefund(ar.getTradeNo());
			Logger.comment("查询部分退款单号");
			MPayment ppm = this.getPPPayment(aqb);
			MRefund mrr = pdw.selectRefundRequestByPaymentId(ppm.getPaymentId()).get(0);
			String refundbn = mrr.getRefundBatchNo().toString();
			String refundid = mrr.getRefundId().toString();
			pdw.updateRefundStatus(refundbn, "0");
			List<MRefund> list = pdw.selectRefundRequestByTraceId(ar.getRefundNo());
			godrefundcompensateCall.addHeader("Mock", "1");
			godrefundcompensateCall.addHeader("MockResult-Hipo-is_success", "true");
			String mockid = TestDataManager.getRandomUUID().toString();
			godrefundcompensateCall.addHeader("MockId", mockid);
			godrefundcompensateBean.setAppId("job.iapi.ymatou.com");
			godrefundcompensateCall.setData(godrefundcompensateBean);
			godrefundcompensateCall.callService();		
			Thread.sleep(2000);
			list = pdw.selectRefundRequestByTraceId(ar.getRefundNo());
			Logger.verifyEquals(1, list.get(0).getRefundStatus(), "提交第三方退款成功");
//			查询退款单提交第三方
//			select RefundId from refund where paytype = 6000 and CreateTime > date_sub(now(), interval 30 day)
//					and RefundStatus = 0 and ApproveStatus = 1 and IsCancel= 0 limit 30

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GodRefundCompensate_003() {
		Logger.start(true,"神器退款补偿job成功-6107");
		try {
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("6107");
			aqb.setPayPrice("6.00");
			String tn = PayGateWayCallService.acquireOrder(aqb).getString("Result");
			PayGateWayCallService.notifyCall(aqb);
			AcquireRefundBean ar = PayGateWayCallService.AcquireRefund(aqb, "5.00");
			PayGateWayCallService.approveRefund(ar.getTradeNo());
			Logger.comment("查询部分退款单号");
			MPayment ppm = this.getPPPayment(aqb);
			MRefund mrr = pdw.selectRefundRequestByPaymentId(ppm.getPaymentId()).get(0);
			String refundbn = mrr.getRefundBatchNo().toString();
			String refundid = mrr.getRefundId().toString();
			pdw.updateRefundStatus(refundbn, "0");
			List<MRefund> list = pdw.selectRefundRequestByTraceId(ar.getRefundNo());
			godrefundcompensateCall.addHeader("Mock", "1");
			godrefundcompensateCall.addHeader("MockResult-Hipo-is_success", "true");
			String mockid = TestDataManager.getRandomUUID().toString();
			godrefundcompensateCall.addHeader("MockId", mockid);
			godrefundcompensateBean.setAppId("job.iapi.ymatou.com");
			godrefundcompensateCall.setData(godrefundcompensateBean);
			godrefundcompensateCall.callService();		
			Thread.sleep(2000);
			list = pdw.selectRefundRequestByTraceId(ar.getRefundNo());
			Logger.verifyEquals(1, list.get(0).getRefundStatus(), "提交第三方退款成功");
//			查询退款单提交第三方
//			select RefundId from refund where paytype = 6000 and CreateTime > date_sub(now(), interval 30 day)
//					and RefundStatus = 0 and ApproveStatus = 1 and IsCancel= 0 limit 30

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}